/*
*            readScp1000LQ02.pde
*  Read the Barometric Pressure and Temperature values from SCP1000 sensor
* 
*  based on sample code from Arduino forum
*  http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1236443720/8
*/

#include <LiquidCrystal.h>
#include <SCP1000.h>

// Specify slave select pin for SCP1000 device
#define SelectPin 10

#define CELCIUS_SIGN 0
#define ANALOG0 14


SCP1000 scp1000(SelectPin);
// LiquidCrystal(rs, rw, enable, d4, d5, d6, d7) 
LiquidCrystal lcd(8, 6, 7, 5, 4, 3, 2);

byte CEL[8]={
B11000,
B11000,
B00110,
B01001,
B01000,
B01001,
B00110,
};

byte GRAPH[7] = {
  B00000,
  B00000,
  B01000,
  B10100,
  B00011,
  B00000,
  B00000,
};

unsigned int P[25] = {
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,
};

unsigned int B[25]= {
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,
};
unsigned int p_min;
unsigned int p_max;


void setup()
{
  
   Serial.begin(9600); // Open serial connection to report values to host
   Serial.println("Starting up");
   
   lcd.begin(16,2);
   lcd.print("Starting up");
   lcd.createChar(CELCIUS_SIGN,CEL);
   lcd.createChar(1,GRAPH);
   lcd.createChar(2,GRAPH);
   lcd.createChar(3,GRAPH);
   lcd.createChar(4,GRAPH);
   lcd.createChar(5,GRAPH);
  
   scp1000.init();
   
   pinMode(ANALOG0, OUTPUT);
   
   lcd.setCursor(0,0);
   lcd.write(1);
   lcd.write(2);
   lcd.write(3);
   lcd.write(4);
   lcd.write(5);

   scp1000.readSensor();
   delay(100);


   
}

void loop()
{
  unsigned int val;
  float tempF;
  unsigned int hpaI;
  float hpaF;

  digitalWrite(ANALOG0,HIGH);

  shiftValues();
  scp1000.readSensor();
  val = scp1000.BaroP;
  P[24] = val;
  calcMax();
  setGraphData();

   lcd.setCursor(0,0);
   /*
   lcd.write(1);
   lcd.write(2);
   lcd.write(3);
   lcd.write(4);
   lcd.write(5);
   */

  
  lcd.setCursor(5,0);
  lcd.print(val);
  lcd.print("hPa ");
  val = scp1000.TempC;
  lcd.print(val);
  lcd.write(CELCIUS_SIGN);

  digitalWrite(ANALOG0,LOW);
  
  /*
  Serial.print("Temprature (C/F): ");
  Serial.print(scp1000.TempC);
  Serial.print(" / ");
  Serial.println( (1.8*scp1000.TempC + 32) );
  Serial.print("Pressure (hPa): ");
  Serial.println(scp1000.BaroP);
  Serial.println("---------------------------------");
  */

  // wait one hour
  delay(3600000UL);
}
  
void shiftValues() {
  for ( int i = 0 ; i < 24 ; i++) {
    P[i] = P[i+1];
  }
}

void calcMax() {
  p_min = P[24];
  p_max = P[24];
  for ( int i = 0 ; i <= 23 ; i++) {
    if (P[i] > p_max)
    {
      p_max = P[i];
    }
    if ((P[i] < p_min) && (P[i] > 0))
    {
      p_min = P[i];
    }
  }
}


void setGraphData() {
  int b=0;
  for ( int i = 24 ; i > 0 ; i--) {
    if (P[i] > 0 )
    {
      B[i] = map(P[i], p_min, p_max, 1, 7);
    }
    else 
    {
      B[i]=0;
    }
    Serial.print (P[i]);
    Serial.print (":");
    Serial.println (B[i]);
  }
  
  // loop each char
  for ( int f = 1 ; f <= 5 ; f++) 
  {
    // initialize font data
    for ( int i=0 ; i <= 6 ; i++)
    {
      GRAPH[i] = 0;
    }

    for ( int i = 4 ; i >=0  ; i--) 
    {
      if (B[b] >  0 ) {
        GRAPH[ 7-B[b]] =  (unsigned int)1 << i ;
      }
      b++;
    }
    lcd.createChar(f,GRAPH);
  }  
  // B11000,
  // B11000,
  // B00110,
  // B01001,
  // B01000,
  // B01001,
  // B00110,


  
}





